package com.ruoyi.web.controller.payment.ceshi;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ResultMap;
import com.ruoyi.system.domain.zhuangxiumain.vo.ZhiFuVo;
import com.ruoyi.system.service.vantservice.orderservice.ApiOrderMasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
/**
 * Created by 小董 on 2021/2/2 16:59
 */
@RestController
@RequestMapping("/api/wxPay")
public class WXController {
    @Autowired
    private WXserviceImpl wxPayService;
    @Autowired
    private ApiOrderMasterService apiOrderMasterService;

    /**
     * 统一下单
     * 官方文档:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1
     *
     * @return
     * @throws Exception
     */
//    @PostMapping("/pay")
//    public Map<String, String> wxPay(@RequestBody PayParam param) throws Exception {
////        String attach = "{\"attach\":\"" + param.getType() + "\"}";
//        //attach 是唯一的 额外参数！！！重点重点重点
//        //所以 用json来存多个参数， 转成String然后在解析
//        JSONObject object = new JSONObject();
//        object.put("type",param.getType());
//        object.put("id",param.getId());
//        object.put("money",param.getMoney());
//        String attach = object.toJSONString();
//        //请求预支付订单  可以从自己的数据中查出订单编号
//        String outTradeNo = "";
//        Map<String, String> result = WxPayTool.wxPay(param.getMoney(), outTradeNo, "订单详情",attach);
//        return result;
//    }
//    @PostMapping("/weiXinZhi")
    public AjaxResult weiXinZhi(@RequestBody ZhiFuVo zhiFuVo) throws Exception {
        ZhiFuVo zhiFuVo1 = apiOrderMasterService.weiXinZhiFu(zhiFuVo);
        System.out.println(zhiFuVo1+"41111111111111111");
        JSONObject object = new JSONObject();
        object.put("type",22);
        object.put("id",12);
        object.put("money",200);
        String attach = object.toJSONString();
        Map<String, String> result = WxPayTool.wxPay("123", "123456", "订单详情",null,null,null);
        System.out.println(result);
        return AjaxResult.success(result);
    }


    /**
     * 支付异步结果通知，我们在请求预支付订单时传入的地址
     * 官方文档 ：https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_7&index=3
     */
    @PostMapping(value = "notify")
    public String wxPayNotify(HttpServletRequest request) {
        String resXml = "";
        System.out.println("---------------");
        System.out.println("---------------");
        System.out.println("---------------");
        try {
            InputStream inputStream = request.getInputStream();
            //将InputStream转换成xmlString
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
//                System.out.println(e.getMessage());
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
//                    e.printStackTrace();
                }
            }
            resXml = sb.toString();
            String result = wxPayService.payBack(resXml);
            return result;
        } catch (Exception e) {
//            System.out.println("微信手机支付失败:" + e.getMessage());
            String result = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
            return result;
        }
    }

}
